﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Runtime.Serialization;

namespace Moonlite.ExtensionApi
{
    public class SerializableDictionary : Dictionary<string, bool>
    {
        public void ReadXml(string path)
        {
            XmlDocument document = new XmlDocument();
            document.Load(path);

            foreach (XmlNode node in document.ChildNodes)
            {
                if (node.Name.ToLower() == "dictionary")
                {
                    foreach (XmlNode childNode in node.ChildNodes)
                    {
                        if (childNode.Name.ToLower() == "item")
                        {
                            string key = "";
                            bool value = false;

                            foreach (XmlAttribute attribute in childNode.Attributes)
                            {
                                switch (attribute.Name.ToLower())
                                {
                                    case "key":
                                        key = attribute.Value;
                                        break;
                                    case "value":
                                        value = bool.Parse(attribute.Value);
                                        break;
                                    default:
                                        break;
                                }
                            }

                            Add(key, value);
                        }
                    }
                }
            }
        }

        public void WriteXml(string path)
        {
            XmlDocument document = new XmlDocument();
            XmlNode rootNode = document.CreateNode(XmlNodeType.Element, "Dictionary", null);

            foreach (string key in Keys)
            {
                XmlNode itemNode = document.CreateNode(XmlNodeType.Element, "Item", null);

                XmlAttribute keyAttribute = document.CreateAttribute("Key");
                XmlAttribute valueAttribute = document.CreateAttribute("Value");

                keyAttribute.Value = key;
                valueAttribute.Value = this[key].ToString();

                itemNode.Attributes.Append(keyAttribute);
                itemNode.Attributes.Append(valueAttribute);
                rootNode.AppendChild(itemNode);
            }

            document.AppendChild(rootNode);
            DirectoryHelper.MakeFolderWritable(path);
            using (Stream stream = File.Create(path))
            {
                document.Save(stream);
                stream.Close();
            }
        }

        public SerializableDictionary()
        {

        }

        public SerializableDictionary(int capacity)
            : base(capacity)
        {

        }

        public SerializableDictionary(IEqualityComparer<string> comparer)
            : base(comparer)
        {

        }

        public SerializableDictionary(int capacity, IEqualityComparer<string> comparer)
            : base(capacity, comparer)
        {

        }

        public SerializableDictionary(IDictionary<string, bool> dictionary)
            : base(dictionary)
        {

        }

        public SerializableDictionary(IDictionary<string, bool> dictionary, IEqualityComparer<string> comparer)
            : base(dictionary, comparer)
        {

        }

        protected SerializableDictionary(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {

        }
    }
}